package com.wanlian.dao.impl;

import com.wanlian.dao.UserPhoneCodeDao;
import com.wanlian.entity.UserPhoneCode;
import com.wanlian.util.DateUtil;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
public class UserPhoneCodeDaoImpl extends GenericDaoImpl<UserPhoneCode> implements UserPhoneCodeDao {

    public UserPhoneCodeDaoImpl() {
        super(UserPhoneCode.class);
    }

    @Override
    public UserPhoneCode queryByPhone(String phone) {


        Date now = new Date();
        Date now_15 = new Date(now.getTime() - 1000*15*60); //15分钟前的时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String nowTime_15 = dateFormat.format(now_15);

        StringBuffer sqlWhere = new StringBuffer(" where phone = ? and DATE_FORMAT(?,'%Y-%m-%d %H:%i:%s') < addtime ");
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(phone);
        params.add(nowTime_15);
        String hql=String.format("from %s %s   order by addTime desc ",entityClassName( ),sqlWhere);
        System.out.println(hql);
        List<UserPhoneCode> codes = this.queryForListByPage(hql, params.toArray(),new Page(1,1){});
        if (codes.size() == 1) {
            return codes.get(0);
        }
        return null;
    }
}
